package com.sg.zhui.visitor.activity;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sg.zhui.visitor.R;
import com.sg.zhui.visitor.bean.ActionItem;
import com.sg.zhui.visitor.utils.TakePhotoUtils;
import com.sg.zhui.visitor.widget.PullDownPopup;

import java.io.File;
import java.io.IOException;

//import top.zibin.luban.Luban;
//import top.zibin.luban.OnCompressListener;

/**
 * Created by chenlipeng on 2018/8/7 0007
 * describe:  个人信息
 */
public class PersonalInformationActivity extends AppCompatActivity {


//    @BindView(R.id.tv_head_pic)
//    ImageView tvHeadPic;
//    @BindView(R.id.rl_picture)
//    RelativeLayout rlPicture;
//    @BindView(R.id.iv_pic)
//    ImageView ivPic;
//    @BindView(R.id.tv_ID)
//    EditText tvID;
//    @BindView(R.id.tv_person_type)
//    TextView tvPersonType;
//    @BindView(R.id.tv_back_home_activity)
//    TextView tvBackHomeActivity;
//    @BindView(R.id.root_layout)
//    LinearLayout rootLayout;
    private Dialog mDialog;
    private File takePhotoFile;

    private final int REQUEST_CODE_TAKE_PHOTO = 100;
    private final int REQUEST_CODE_CHOICE_PHOTO = 200;
    //请求裁剪
    private static final int REQUEST_CROP_PHOTO = 102;
    private String photoPath;
    private PullDownPopup popwindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_information);
//        ButterKnife.bind(this);

        //实例化popwindow
        popwindow = new PullDownPopup(PersonalInformationActivity.this, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        popwindow.setItemOnClickListener(new PullDownPopup.OnItemOnClickListener() {
            @Override
            public void onItemClick(ActionItem item, int position) {
                if (position == 0) {

                } else {
                }
            }

        });
        popwindow.addAction(new ActionItem(PersonalInformationActivity.this, "员工手机", R.drawable.ic_back_white));
        popwindow.addAction(new ActionItem(PersonalInformationActivity.this, "员工楼号", R.drawable.ic_back_white));

    }

//    @OnClick({R.id.rl_picture, R.id.tv_person_type})
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//            case R.id.rl_picture:
//                DialogPic();
//                break;
//            case R.id.tv_person_type:
//                popwindow.show(view);
//
//                break;
//        }
//    }

    /**
     * 弹框
     */
    private void DialogPic() {
        mDialog = new Dialog(PersonalInformationActivity.this, R.style.ActionSheetDialogStyle);
        View inflate = LayoutInflater.from(PersonalInformationActivity.this).inflate(R.layout.dialog_pic_gain_visitor, null);

        mDialog.setContentView(inflate);
        Window dialogwindow = mDialog.getWindow();

        dialogwindow.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogwindow.getAttributes();
        lp.windowAnimations = R.style.PopUpBottomAnimation;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;

        dialogwindow.setAttributes(lp);
        mDialog.show();

        TextView tv_close, pat_pic, photo_select;
        tv_close = inflate.findViewById(R.id.tv_close);
        photo_select = inflate.findViewById(R.id.photo_select);
        pat_pic = inflate.findViewById(R.id.pat_pic);
        tv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
            }
        });

        pat_pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//照片
                mDialog.dismiss();
                try {
                    takePhotoFile = TakePhotoUtils.takePhoto(false, null, PersonalInformationActivity.this, "nwbPhotos", REQUEST_CODE_TAKE_PHOTO);
                } catch (IOException e) {
                    Toast.makeText(PersonalInformationActivity.this, "拍照失败", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });
        photo_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//相册
                mDialog.dismiss();
                //从相册选择
//                startActivityForResult(BGAPhotoPickerActivity.newIntent(PersonalInformationActivity.this, null, 1, null, false), REQUEST_CODE_CHOICE_PHOTO);
            }
        });
    }

    private void modifyAvatar() {
//        Luban.with(this)
//                .load(new File(photoPath))                     //传人要压缩的图片
//                .setCompressListener(new OnCompressListener() {
//                    @Override
//                    public void onStart() {
//                        //  压缩开始前调用，可以在方法内启动 loading UI
//                    }
//
//                    @Override
//                    public void onSuccess(File file) {
//                        //  压缩成功后调用，返回压缩后的图片文件
//
////                        Glide.with(PersonalInformationActivity.this).load(photoPath)
////                                .asBitmap().dontAnimate().fitCenter()
////                                .into(tvHeadPic);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        //  当压缩过程出现问题时调用
//                    }
//                }).launch();    //启动压缩
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        //拍照回调
        if (requestCode == REQUEST_CODE_TAKE_PHOTO && resultCode == RESULT_OK) {
            if (takePhotoFile != null) {
                photoPath = takePhotoFile.getAbsolutePath();

//                gotoClipActivity(Uri.fromFile(takePhotoFile));
                modifyAvatar();
                // 对图片进行裁剪

            }
        }

        //选择图片回调
        if (requestCode == REQUEST_CODE_CHOICE_PHOTO && resultCode == RESULT_OK) {
//            try {
//                photoPath = BGAPhotoPickerActivity.getSelectedImages(data).get(0);
//
////                gotoClipActivity(Uri.parse(photoPath));
//                modifyAvatar();
//                // 对图片进行裁剪
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
        }

        //裁剪
        if (requestCode == REQUEST_CROP_PHOTO && resultCode == RESULT_OK) {
            final Uri uri = data.getData();
            if (uri == null) {
                return;
            }
        }

    }
}
